package com.uphone.magicleafteacher.pro.activity;

import android.arch.lifecycle.LifecycleOwner;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SizeUtils;
import com.radish.baselibrary.Intent.IntentData;
import com.radish.baselibrary.Intent.IntentUtils;
import com.radish.framelibrary.view.glide.GlideUtils;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
import com.uphone.magicleafteacher.MyApp;
import com.uphone.magicleafteacher.R;
import com.uphone.magicleafteacher.base.mvp.BaseMvpActivity;
import com.uphone.magicleafteacher.http.ApiService;
import com.uphone.magicleafteacher.http.FailureConsumer;
import com.uphone.magicleafteacher.http.RxSchedulers;
import com.uphone.magicleafteacher.http.SuccessfulConsumer;
import com.uphone.magicleafteacher.pro.activity.listenertraining.ListenerTrainingContract;
import com.uphone.magicleafteacher.pro.activity.listenertraining.ListenerTrainingPresenter;
import com.uphone.magicleafteacher.pro.adapter.RehearsalAdapter;
import com.uphone.magicleafteacher.util.CommonUtil;
import com.uphone.magicleafteacher.util.GsonUtils;
import com.uphone.magicleafteacher.vo.RehearsalBean;
import com.uphone.magicleafteacher.vo.WorkcorrectionEvent;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

public class RehearsalActivity extends BaseMvpActivity<ListenerTrainingPresenter> implements ListenerTrainingContract.View {
    @BindView(R.id.img_book)
    ImageView imgBook;
    @BindView(R.id.text_bookname)
    TextView textBookname;
    @BindView(R.id.text_bookauthor)
    TextView textBookauthor;
    @BindView(R.id.text_bookpress)
    TextView textBookpress;
    @BindView(R.id.text_lookboook)
    LinearLayout textLookboook;
    @BindView(R.id.recy_stu)
    RecyclerView recyclerView;
    @IntentData
    private int id;

    @Override
    protected void initLayoutBefore() {
        super.initLayoutBefore();
        isEventBus = true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRefresh(WorkcorrectionEvent outGroupEvent) {
        //刷新
        initData();
    }


    private RehearsalAdapter mAdapter;

    @Override
    protected void initInject() {

    }

    @Override
    protected void initBundle() {

    }

    @Override
    protected int initLayout() {
        return R.layout.activity_rehearsal;
    }

    @Override
    protected void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new RehearsalAdapter();
        recyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void initTitle() {
        initTitle("复述详情");
    }

    @Override
    protected void initData() {
        MyApp.apiService(ApiService.class)
                .repeatDataDetail(CommonUtil.getTeacherId(), "" + id)
                .compose(RxSchedulers.io_main())
                .doOnSubscribe(d -> {
                    showLoading();
                })
                .doFinally(() -> {
                    closeLoading();
                })
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from((LifecycleOwner) this)))
                .subscribe(new SuccessfulConsumer() {
                    @Override
                    public void success(String jsonObject) {
                        RehearsalBean bean = GsonUtils.getGson().fromJson(jsonObject, RehearsalBean.class);
                        if (bean != null && bean.getRepeatDetail() != null) {
                            loadData(bean.getRepeatDetail());
                        }
                    }
                }, new FailureConsumer());
    }

    private void loadData(RehearsalBean.RepeatDetailBean bean) {
        GlideUtils.getInstance().loadCornerImage(this, CommonUtil.getImageUrl(bean.getBookPic()), SizeUtils.dp2px(5), imgBook);
        textBookname.setText(CommonUtil.getStr(bean.getBookName()));
        textBookauthor.setText(CommonUtil.getStr(bean.getAuthor()));
        textBookpress.setText(CommonUtil.getStr(bean.getPublishCompany()));
        mAdapter.setNewData(bean.getFinishedStudents());
    }

    @Override
    protected void initListener() {
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            IntentUtils.getInstance().with(this, RetellCorrectActivity.class)
                    .putInt("status", mAdapter.getItem(position).getIsRemark())
                    .putInt("id", mAdapter.getItem(position).getAnswerId())
                    .start();

        });
    }


    @OnClick(R.id.text_lookboook)
    public void onViewClicked() {
        CommonUtil.startIntent(this, VoiceActivity.class, id);
    }

    @Override
    public void authAudioWork(int auth) {

    }
}
